1073D. Berland Fair

1073D. Berland Fair

每次先走一圈,計算出可以購買的糖果的總價格 c 和總個數 t,然後 cnt += t*(T/c)  T %= c,循環至無糖果可買。

時間複雜度計算如下:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 5;
typedef long long ll;
int n,p[maxn];
ll T;

void get(ll &T,ll &c,ll &cnt){
    for(int i = 0; i < n;i++){
        if(T >= p[i]){
            T -= p[i];
            c += p[i];
            cnt++;
        }
    }
}
int main()
{
    // freopen("data.in","r",stdin);
    // freopen("data.out","w",stdout);
    scanf("%d%lld",&n,&T);
    ll cnt = 0;
    int m = 1<<30;
    for(int i = 0; i < n; i++){
        scanf("%d",&p[i]);
        m = min(m,p[i]);
    }
    while(T >= m){
        ll c = 0, t = 0;
        get(T,c,t);
        cnt += t*(T/c) + t;
        T %= c;
    }
    printf("%lld\n",cnt);

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章